package com.crm.system.controller.productManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.productManager.CustomerProductImportVO;
import com.crm.model.vo.contractReviewManager.ContractImportVO;
import com.crm.system.service.cluePoolManager.ExcelUtil;
import com.crm.system.service.customerManager.CustomerImportService;
import com.crm.system.service.productManager.CustomerProductImportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author luojie
 * @date 2021/6/29 15:17
 */
@RestController
@RequestMapping("/product")
@Slf4j
public class CustomerProductImportController {

    @Autowired
    private CustomerProductImportService customerProductImportService;

    /***
     * @Description:导入产品接口
     * @Author: luojie
     * @Date: 2021/6/29 16:33
     */
    @PostMapping("/productImport")
    public DataResponse productImport(HttpServletRequest request,
                                       @RequestParam MultipartFile file) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<CustomerProductImportVO> list = excelUtil.excelImport(file.getInputStream(), CustomerProductImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }

            jsonObject = customerProductImportService.productImport(list);

        } catch (Exception e) {
            log.error("contractImport error:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }
}
